package com.pda.xinma.ui.picking;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.pda.xinma.R;
import com.pda.xinma.bean.PickInfoBean;
import com.pda.xinma.bean.ScanBean;
import com.pda.xinma.bean.ScanResultBean;
import com.pda.xinma.bean.request.PickingRequest;
import com.pda.xinma.contract.PickingContract;
import com.pda.xinma.dialog.AffirmDialog;
import com.pda.xinma.presenter.PickingPresenter;
import com.pda.xinma.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.base.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.PICKINGACTIVITY)
public class PickingActivity extends BaseMvpActivity<PickingPresenter> implements PickingContract.View {
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_container)
    TextView tvContainer;
    @BindView(R.id.tv_latheName)
    TextView tvLatheName;
    @BindView(R.id.tv_processName)
    TextView tvProcessName;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_model)
    TextView tvModel;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    @BindView(R.id.tv_pickNum)
    TextView tvPickNum;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_picking)
    TextView tvPicking;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    @BindView(R.id.tv_detail)
    TextView tvDetail;

    @Override
    public PickingPresenter getPresenter() {
        return new PickingPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_picking;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                if (!TextUtils.isEmpty(scanBean.getCode())) {
                    if (scanBean.getCode().contains("order")) {
                        if (TextUtils.isEmpty(tvStorageBin.getText().toString())) {
                            showMessage("请先扫接驳位");
                            return;
                        }
                        int lenth = scanBean.getCode().indexOf("A");
                        String order = scanBean.getCode().substring(lenth + 1);
                        tvContainer.setText(order);
                        if (!TextUtils.isEmpty(tvStorageBin.getText().toString())) {
                            presenter.getPickInfoByOrderAndBin(tvContainer.getText().toString(), tvStorageBin.getText().toString());
                        }
                    } else {
                        presenter.scanTheQRCodeToVerify(scanBean.getCode());
                    }
                }
            }
        });
    }

    @Override
    public void getData() {

    }

    PickInfoBean bean;

    @Override
    public void setData(PickInfoBean pickInfoBean) {
        this.bean = pickInfoBean;
        tvArticleId.setText(pickInfoBean.getArticleId());
        tvGrade.setText(pickInfoBean.getGrade());
        tvModel.setText(pickInfoBean.getModel());
        tvLatheName.setText(pickInfoBean.getLatheName());
        tvPickNum.setText(pickInfoBean.getPickNum());
        tvProcessName.setText(pickInfoBean.getProcessName());
        if (pickInfoBean.getButtonShow().equals("10")) {
            tvPicking.setVisibility(View.VISIBLE);
            tvMoveOut.setVisibility(View.GONE);
        }
        if (pickInfoBean.getButtonShow().equals("20")) {
            tvPicking.setVisibility(View.GONE);
            tvMoveOut.setVisibility(View.VISIBLE);
        }
        if (pickInfoBean.getButtonShow().equals("30")) {
            tvPicking.setVisibility(View.VISIBLE);
            tvMoveOut.setVisibility(View.VISIBLE);
        }
        if (pickInfoBean.getButtonShow().equals("40")) {
            tvPicking.setVisibility(View.GONE);
            tvMoveOut.setVisibility(View.GONE);
        }
    }

    @Override
    public void setScanBean(ScanResultBean scanBean) {
        if (scanBean.getType().equals("KW")) {
            tvStorageBin.setText(scanBean.getCode());
            if (!TextUtils.isEmpty(tvContainer.getText().toString())) {
                presenter.getPickInfoByOrderAndBin(tvContainer.getText().toString(), tvStorageBin.getText().toString());
            }
        } else /*if ()*/ {
            ToastUtils.showToast(mContext, "请扫接驳工位？");
        }
    }

    @Override
    public void setSuccess() {
        bean = null;
        tvStorageBin.setText("");
        tvContainer.setText("");
        tvArticleId.setText("");
        tvGrade.setText("");
        tvModel.setText("");
        tvLatheName.setText("");
        tvPickNum.setText("");
        tvProcessName.setText("");
        tvPicking.setVisibility(View.GONE);
        tvMoveOut.setVisibility(View.GONE);
    }


    @OnClick({R.id.iv_close, R.id.tv_picking, R.id.tv_move_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                finishActivity();
                break;
            case R.id.tv_picking:
                if (bean != null) {
                    AffirmDialog dialog = new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                        @Override
                        public void onConfrim() {
                            PickingRequest request = new PickingRequest();
                            request.setConnectionCode(tvStorageBin.getText().toString());
                            request.setDeliveryDetailId(bean.getDeliveryDetailId());
                            presenter.confirmPick(request);
                        }
                    });
                    dialog.show("确认拣货吗？");
                }
                break;
            case R.id.tv_move_out:
                AffirmDialog dialog = new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                    @Override
                    public void onConfrim() {
                        PickingRequest request = new PickingRequest();
                        request.setConnectionCode(tvStorageBin.getText().toString());
                        request.setDeliveryDetailId(bean.getDeliveryDetailId());
                        presenter.confirmBack(request);
                    }
                });
                dialog.show("确认退回吗？");
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.tv_detail)
    public void onViewClicked() {
        if (TextUtils.isEmpty(tvContainer.getText().toString())) {
            showMessage("请先扫生产订单码");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putString("order", tvContainer.getText().toString());
        startIntent(AppActivityKey.ORDERDETAILACTIVITY, bundle);
    }
}
